package cn.com.checne.controller.superManager;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.checne.dao.WebsiteInfoDao;
import cn.com.checne.model.domain.WebsiteInfo;
import cn.com.checne.util.PageBean;
import cn.com.checne.util.WebUtils;

@Controller
@RequestMapping(value = "/super/website")
public class WebsiteInfoManagerController {

    protected static final Logger logger = Logger.getLogger(WebsiteInfoManagerController.class);
    @Resource
	private WebsiteInfoDao websiteInfoDao;
	
	//站点同步--首页
	@RequestMapping(value="/index.do")
	public String index(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "super/frame/frame_synch";
	}
	
	//同步--左侧导航
	@RequestMapping(value="/left.do")
	public String left(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "super/synch/left";
	}

	
    //站点管理--列举所有的栏目
    @RequestMapping(value = "/website-list.do")
    public ModelAndView navigateList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
        ModelAndView mav = new ModelAndView("super/synch/website-list");
		int curPage = WebUtils.getRequestParameterAsInt(request, "curPage", 1);

		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		List<WebsiteInfo> websiteList = this.websiteInfoDao.getSubList(null,pageBean.getStart(), pageBean.getRowsPerPage());
		pageBean.setMaxRowCount(this.websiteInfoDao.getCount(null));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		modelMap.put("websiteList", websiteList);
		modelMap.put("pageBean", pageBean);
		mav.addAllObjects(modelMap);
		return mav;
    }

    //站点管理--站点新增、修改页面
    @RequestMapping(value = "/website-show.do")
    public ModelAndView websiteShow(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
        ModelAndView mav = new ModelAndView("super/synch/website-show");
        int websiteId = WebUtils.getRequestParameterAsInt(request, "websiteId", 0);
		WebsiteInfo website = new WebsiteInfo();
		String opt = "create";
		if (websiteId > 0) {
			website = this.websiteInfoDao.getWebsite(websiteId);
            opt = "update";
        }
		modelMap.put("website", website);
        modelMap.put("opt", opt);
		mav.addAllObjects(modelMap);
		return mav;
    }

    //站点管理--站点更新
    @RequestMapping(value = "/website-update.do")
    public ModelAndView navigateUpdate(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
        ModelAndView mav = new ModelAndView();
        String message;
        int id = WebUtils.getRequestParameterAsInt(request, "id", 0);
		String name = WebUtils.getRequestParameterAsString(request, "name");
		String desc = WebUtils.getRequestParameterAsString(request, "desc");
		String sign = WebUtils.getRequestParameterAsString(request, "sign");
		WebsiteInfo website = this.websiteInfoDao.getWebsite(id);
		website.setName(name);
		website.setDesc(desc);
		website.setSign(sign);
		
        if (websiteInfoDao.update(website)) {
			message = "分站信息更新完毕,请进行分站信息同步!";
        } else {
			message = "分站信息更新失败!";
        }
        modelMap.put("message", message);
        mav.setViewName("common/message");
        mav.addAllObjects(modelMap);
        return mav;
    }
}